"""
This script transforms learning data into SVM learning format, example fragment below:
1 0.979619633405 0.8 0.6 0.4
0 0.324105978828 0.8 0.6 0.4
1 0.99999933629 0.8 0.6 0.4
0 0.95748678623 0.6 0.3 0.4
1 0.432784920572 0.8 0.6 0.4
"""

# I/O modules
import Parser, Post, FileIO
# Quantifiers modules
import NaiveBayes, NotConstructiveQuantifier, NotAQuestionQuantifier, TooLocalizedQuantifier

if __name__ == "__main__":

	output_filename = "Output/SVM_learning3.dat"

	# This will take first command line argument as an output file name
	import sys
	try:
		output_filename = sys.argv[1]
	except:
		pass

	print "Naive Bayes learning, this may take a long time ..."
	wordBag = NaiveBayes.train()
	print "Done."	

	f_out = open(output_filename, 'w')

	fileStream = FileIO.openTrainingFile(FileIO.READ_MODE)
	Parser.readHeader(fileStream)

	print "Will start writing output file, this may take a long time, please wait ..."
	while (not Parser.isEndOfFile(fileStream)):
		
		newPost = Parser.readPost(fileStream)
		bodyMarkdown = newPost.bodyMarkdown

		newString = str( newPost.openStatus ) + " "

		newString += str( NaiveBayes.calculateProbability(wordBag, newPost).offTopic ) + " " + str(NotConstructiveQuantifier.quantify(newPost)) + " " + str(NotAQuestionQuantifier.quantify(bodyMarkdown)) + " " + str(TooLocalizedQuantifier.quantify(newPost)) + "\n"

		f_out.write(newString)

	print "Finished."
	f_out.close()